package com.L.yygh.cmn.controller;

import com.L.yygh.cmn.listener.DictExcellListener;
import com.L.yygh.cmn.service.DictService;
import com.L.yygh.cmn.service.impl.DictServiceImpl;
import com.L.yygh.common.result.Result;
import com.L.yygh.model.cmn.Dict;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Api(tags = "数据字典接口")
@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {

    @Autowired
    private DictService dictService;


    @ApiOperation("根据字典数据id查询子数据")
    @GetMapping("/findChild/{id}")
    public Result findChildDictData(@PathVariable("id") Long id){
        List<Dict> childDictData = dictService.findChildDictData(id);
        return Result.ok(childDictData);

    }

    @ApiOperation("以Excel格式导出字典数据")
    @GetMapping("exportDictExcel")
    public void exportData(HttpServletResponse response){
        dictService.exportDictExcel(response);
    }

    @PostMapping("importDictExcel")
    public Result importData(MultipartFile file) throws IOException {
        dictService.importDictExcel(file);
        return Result.ok();
    }

    //根据dictcode和value查询
    @GetMapping("/getName/{dictCode}/{value}")
    public String getName(@PathVariable("dictCode") String dictCode,
                          @PathVariable("value") String value){
        String dictName = dictService.getDictName(dictCode,value);
        return dictName;
    }

    //根据value查询
    @GetMapping("/getName/{value}")
    public String getName(@PathVariable("value") String value){
        String dictName = dictService.getDictName("",value);
        return dictName;
    }

    //根据dictCode获取下级节点
    @ApiOperation("根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictCode}")
    public Result findByDictCode(@PathVariable("dictCode") String dictCode){
        List<Dict> list = dictService.findByDictCode(dictCode);
        return Result.ok(list);
    }
}
